package ink.xiaobaibai.common;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import ink.xiaobaibai.entity.Setting;
import ink.xiaobaibai.service.ISettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description: 全局配置键
 * @author: 小白白
 * @create: 2021-05-22
 **/

@Component
public class SettingCommon {

    //已注入
    private static ISettingService settingService;

    private static StringRedisTemplate stringRedisTemplate;

    private static final Map<String, Object> SETMAP = new HashMap<>();

    /**
     * KEY们
     */
    public static final String DEPOSIT_KEY = "押金金额";

    public static final String MONTHLY_FEE_KEY = "月费金额";

    public static final String USE_AGREEMENT = "使用协议";

    public static final String CAROUSEL_IMAGE = "轮播图";

    public static final String MAP_KEY = "地图信息";

    public static final String ONEPAY_AGREEMENT = "定点协议";

    public static final String DEPOSIT_AGEEMENT = "押金协议";

    public static final String MONTHLY_AGGEMENT = "月费协议";

    public static final String BUY_AGGEMENT = "购买协议";

    public static final String MAIN_IMAGE_INFO = "主界面四张图文";

    public static final String ESCROW_AGREEMENT = "托管协议";

    public static final String ABOUT_KEY = "关于我们";

    /**
     * 获取押金金额
     */
    public static BigDecimal getDepositKey() {
        Object o = SETMAP.get(DEPOSIT_KEY);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(DEPOSIT_KEY);
            if (obj != null)
                return (BigDecimal) o;
        }
        BigDecimal bigDecimal = new BigDecimal(getValue(DEPOSIT_KEY).toString());
        SETMAP.put(DEPOSIT_KEY, bigDecimal);
        return bigDecimal;
    }

    /**
     * 获取月费金额
     */
    public static BigDecimal getMonthlyFee() {
        Object o = SETMAP.get(MONTHLY_FEE_KEY);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(MONTHLY_FEE_KEY);
            if (obj != null)
                return (BigDecimal) o;
        }
        BigDecimal bigDecimal = new BigDecimal(getValue(MONTHLY_FEE_KEY).toString());
        SETMAP.put(MONTHLY_FEE_KEY, bigDecimal);
        return bigDecimal;
    }

    /**
     * 获取使用协议
     */
    public static String getUseAgreement() {
        Object o = SETMAP.get(USE_AGREEMENT);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(USE_AGREEMENT);
            if (obj != null)
                return o.toString();
        }
        String s = getValue(USE_AGREEMENT).toString();
        SETMAP.put(USE_AGREEMENT, s);
        return s;
    }

    /**
     * 获取轮播图
     */
    public static List<String> getCarouselImage() {
        Object o = SETMAP.get(CAROUSEL_IMAGE);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(CAROUSEL_IMAGE);
            if (obj != null)
                return (List<String>) o;
        }
        String s = getValue(CAROUSEL_IMAGE).toString();
        List<String> list = JSON.parseArray(s, String.class);
        SETMAP.put(CAROUSEL_IMAGE, list);
        return list;
    }

    /**
     * 获取地图信息
     */
    public static MapInfo getMapInfo() {
        Object o = SETMAP.get(MAP_KEY);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(MAP_KEY);
            if (obj != null)
                return (MapInfo) o;
        }
        String s = getValue(MAP_KEY).toString();
        MapInfo mapInfo = JSON.parseObject(s, MapInfo.class);
        SETMAP.put(MAP_KEY, mapInfo);
        return mapInfo;
    }

    /**
     * 定点协议
     */
    public static String getOnepayAgreement() {
        Object o = SETMAP.get(ONEPAY_AGREEMENT);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(ONEPAY_AGREEMENT);
            if (obj != null)
                return o.toString();
        }
        String s = getValue(ONEPAY_AGREEMENT).toString();
        SETMAP.put(ONEPAY_AGREEMENT, s);
        return s;
    }

    /**
     * 押金协议
     */
    public static String getDepositAgeement() {
        Object o = SETMAP.get(DEPOSIT_AGEEMENT);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(DEPOSIT_AGEEMENT);
            if (obj != null)
                return o.toString();
        }
        String s = getValue(DEPOSIT_AGEEMENT).toString();
        SETMAP.put(DEPOSIT_AGEEMENT, s);
        return s;
    }

    /**
     * 包月协议
     */
    public static String getMonthlyAggement() {
        Object o = SETMAP.get(MONTHLY_AGGEMENT);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(MONTHLY_AGGEMENT);
            if (obj != null)
                return o.toString();
        }
        String s = getValue(MONTHLY_AGGEMENT).toString();
        SETMAP.put(MONTHLY_AGGEMENT, s);
        return s;
    }

    /**
     * 购买协议
     */
    public static String getBuyAggement() {
        Object o = SETMAP.get(BUY_AGGEMENT);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(BUY_AGGEMENT);
            if (obj != null)
                return o.toString();
        }
        String s = getValue(BUY_AGGEMENT).toString();
        SETMAP.put(BUY_AGGEMENT, s);
        return s;
    }

    /**
     * 入口页四张图文
     */
    public static List<ImageInfo> getMainImageInfo() {
        Object o = SETMAP.get(MAIN_IMAGE_INFO);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(MAIN_IMAGE_INFO);
            if (obj != null)
                return (List<ImageInfo>) o;
        }
        String s = getValue(MAIN_IMAGE_INFO).toString();
        List<ImageInfo> imageInfos = JSON.parseArray(s, ImageInfo.class);
        SETMAP.put(MAIN_IMAGE_INFO, imageInfos);
        return imageInfos;
    }

    /**
     * 托管协议
     */
    public static String getEscrowAgreement() {
        Object o = SETMAP.get(ESCROW_AGREEMENT);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(ESCROW_AGREEMENT);
            if (obj != null)
                return o.toString();
        }
        String s = getValue(ESCROW_AGREEMENT).toString();
        SETMAP.put(ESCROW_AGREEMENT, s);
        return s;
    }

    /**
     * 关于我们
     */
    public static String getAbout() {
        Object o = SETMAP.get(ABOUT_KEY);
        if (o != null) {
            String obj = stringRedisTemplate.opsForValue().get(ABOUT_KEY);
            if (obj != null)
                return o.toString();
        }
        String s = getValue(ABOUT_KEY).toString();
        SETMAP.put(ABOUT_KEY, s);
        return s;
    }


    public static Object getValue(String key) {
        QueryWrapper<Setting> q1 = new QueryWrapper<>();
        q1.eq("setting_key", key);
        stringRedisTemplate.opsForValue().set(key, "1", 30, TimeUnit.DAYS);
        return settingService.getOne(q1).getSettingValue();
    }

    public static void setValue(String key, String value) {
        UpdateWrapper<Setting> u1 = new UpdateWrapper<>();
        u1.set("setting_value", value);
        u1.eq("setting_key", key);
        settingService.update(u1);
        stringRedisTemplate.delete(key);
    }

    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        SettingCommon.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setSettingService(ISettingService settingService) {
        SettingCommon.settingService = settingService;
    }

}
